const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  reqConsumerDeviceList,
  reqConsumerRoomUpdate,
} from '../../../../request/api.js';
Page({


  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离

    form: {},

    dataList1: [],
    dataList2: [],
    dataList2Length: 0,

    showInputDialog: false
  },

  onLoad(options) {
    this.getParams()
    // this.getDataList()
  },

  onShow() {},


  getParams() {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendDataToDetail', function (res) {
      _this.setData({
        form: res.data
      })
      _this.getDataList()
      console.log('form', _this.data.form);
    })
  },

  async getDataList() {
    let params = {
      homeId: this.data.form.homeId,
      // roomId: this.data.form.id,
      // homeId: '1906590101087543298',
      // roomId: '1906590101158846466',
    }
    const res = await reqConsumerDeviceList(params)
    if (res.code !== 20000) return showMsg(res.message)
    let {
      rows
    } = res.data

    let dataList1 = []
    let dataList2 = []

    rows.forEach(item => {
      if (item.roomId === this.data.form.id) {
        dataList1.push(item)
      } else {
        if (item.roomId) {
          item.dec = `将从${item.roomName}位置移除`
        }
        dataList2.push(item)
      }
    })

    console.log(dataList1,
      dataList2);
    this.setData({
      dataList1,
      dataList2,
      dataList2Length: dataList2.length
    })

  },

  onRoomName() {
    this.setData({
      showInputDialog: true
    })
  },

  async inputDialogHandle(e) {
    const {
      actionType,
      value
    } = e.detail
    if (actionType === 'confirm') {
      if (!value) return showMsg("请填写房间名称")
      this.setData({
        "form.roomName": value
      })
      await this.reqConsumerRoomUpdateFoo()
      this.setData({
        showInputDialog: false
      })
    } else {
      this.setData({
        showInputDialog: false
      })
    }
  },

  onDecreaseDevice(e) {
    const {
      item
    } = e.currentTarget.dataset
    let {
      dataList1,
      dataList2
    } = this.data

    dataList2 = [...dataList2, item]
    dataList1 = dataList1.filter(data => data.id != item.id)
    this.setData({
      dataList1,
      dataList2
    })
  },
  onAddDevice(e) {
    const {
      item
    } = e.currentTarget.dataset
    let {
      dataList1,
      dataList2
    } = this.data

    dataList1 = [...dataList1, item]
    dataList2 = dataList2.filter(data => data.id != item.id)
    this.setData({
      dataList1,
      dataList2
    })
  },

  async confirm() {
    await this.reqConsumerRoomUpdateFoo()
    wx.navigateBack()
  },


  async reqConsumerRoomUpdateFoo() {
    const {
      form,
      dataList1
    } = this.data
    const deviceCodeList = dataList1.map(item => item.deviceCode)
    const res = await reqConsumerRoomUpdate({
      roomId: form.id|| form.roomId,
      roomName: form.roomName,
      userId: app.globalData.userId,
      deviceCodeList,
    })
    if (res.code != 20000) return showMsg(res.message);
    // showMsg("修改成功", 'success')
    app.globalData.isUpdateCIndex = true
    wx.setStorageSync('isUpdateCIndex', true);

    setTimeout(_ => {
      this.notifyPageUpdataFoo()
    }, 1000)
  },

  notifyPageUpdataFoo() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('receiveDataFromDetail', {
      isUpdataList: true
    });
  },

})